package com.atguigu.cloudalibaba.controller;

import com.atguigu.cloudalibaba.entities.Pay;
import com.atguigu.cloudalibaba.entities.PayDTO;
import com.atguigu.cloudalibaba.resp.ResultData;
import com.atguigu.cloudalibaba.resp.ReturnCodeEnum;
import com.atguigu.cloudalibaba.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zzyy
 * @create 2023-12-12 22:34
 */
@RestController
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {

    @Resource
    private PayService payService;

    /**
     * 新增支付流水
     * @param pay 支付流水对象
     * @return 操作结果
     */
    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法, json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + i);
    }

    /**
     * 删除支付流水
     * @param id 支付流水ID
     * @return 操作结果
     */
    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    /**
     * 修改支付流水
     * @param payDTO 支付流水DTO对象
     * @return 操作结果
     */
    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值：" + i);
    }

    /**
     * 根据ID查询支付流水
     * @param id 支付流水ID
     * @return 支付流水对象
     */
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if (id == -4) throw new RuntimeException("id不能为负数");

        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    /**
     * 查询所有支付流水
     * @return 所有支付流水列表
     */
    @GetMapping("/pay/getall")
    @Operation(summary = "查询所有支付流水", description = "查询所有支付流水方法")
    public ResultData<List<Pay>> getAllPays() {
        List<Pay> pays = payService.getAll();
        return ResultData.success(pays);
    }

    /**
     * 测试异常处理
     * @return 异常处理结果
     */
    @RequestMapping(value = "/pay/error", method = RequestMethod.GET)
    public ResultData<Integer> getPayError() {
        Integer i = Integer.valueOf(200);
        try {
            System.out.println("--------come here");
            int data = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return ResultData.success(i);
    }


    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    private String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo)
    {
        return "atguiguInfo: "+atguiguInfo+"\t"+"port: "+port;
    }


}
